import { isObject } from "../shared/index"
import { mutableHandlers } from "./baseHandlers";





export function reactive(target) {
  // 将目标变成响应式对象
  return createReactiveObject(target, mutableHandlers) // 核心就是读取文件时做依赖收集，变化后要重新执行effect
}

const proxyMap = new WeakMap()


function createReactiveObject(target, baseHandlers){

  if(!isObject(target)) {
    return target
  }
  const exisitingProxy = proxyMap.get(target);
  if(exisitingProxy) {
    return exisitingProxy
  }

  const proxy = new Proxy(target, baseHandlers);
  proxyMap.set(target,proxy); // 将代理的对象和代理后的结果做一个映射表

  return proxy
}